package com.realgodo.site.service.communication;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.realgodo.site.entity.application.communication.Commentary;
import com.realgodo.site.entity.application.communication.Reply;
import com.realgodo.site.service.application.ApplicationManager;

@Service
@Transactional
public class ReplyManager extends CommunicationManager<Reply> {
	
	/**发布回复*/
	public Reply saveReply(Reply reply){
		if(reply==null){
			return null;
		}
		String content=reply.getContent();
		if(reply.getId()!=null&&!Long.valueOf(-1).equals(reply.getId())){
			reply=this.get(reply.getId());
		}else{
			Commentary commentary=(Commentary)sessionFactory.getCurrentSession().get(Commentary.class, reply.getCommentary().getId());
			reply.setCommentary(commentary);
			reply.setPubTime(new Date());
		}
		reply.setContent(ApplicationManager.convert(content));
		this.save(reply);
		return reply;
	}
	
	/**查找回复列表（非分页）*/
	@SuppressWarnings("unchecked")
	public List<Reply> findReplyList(Map<String,Object> param){
		Criteria c = sessionFactory.getCurrentSession().createCriteria(Reply.class);
		c.add(Restrictions.eq("commentary.id", Long.valueOf((String)getParamValue(param, "communicationId"))));
		return c.list();
	}
}
